import { request } from "../request";

// 开始轮询视频状态的请求参数接口
export interface StartPollingRequest {
    requestId: string;
    email: string;
}

// 开始轮询视频状态的响应接口
export interface StartPollingRes {
    /* 响应码 */
    code: number;

    /* 响应消息 */
    message: string;

    /* 响应数据 */
    data: {
        /* 是否成功 */
        success: boolean;

        /* 消息 */
        message: string;

        /* 任务ID */
        taskId: string;

        /* 请求ID */
        requestId: string;
    };

    /* 时间戳 */
    timestamp: Record<string, unknown>;
}

// 查询轮询任务状态的响应接口
export interface PollingStatusRes {
    /* 响应码 */
    code: number;

    /* 响应消息 */
    message: string;

    /* 响应数据 */
    data: {
        /* 任务ID */
        taskId: string;

        /* 请求ID */
        requestId: string;

        /* 邮箱 */
        email: string;

        /* 任务状态: POLLING(轮询中) | COMPLETED(完成) | FAILED(失败) | TIMEOUT(超时) | ERROR(错误) */
        status: string;

        /* 创建时间 */
        createTime: number;

        /* 最后检查时间 */
        lastCheckTime?: number;

        /* 检查次数 */
        checkCount?: number;

        /* 视频URL (完成时) */
        videoUrl?: string;

        /* 错误信息 (失败时) */
        errorMessage?: string;

        /* API响应 */
        apiResponse?: Record<string, unknown>;
    };

    /* 时间戳 */
    timestamp: Record<string, unknown>;
}

/**
 * 开始轮询视频生成状态
 * @param requestData 包含requestId和email的请求参数
 * @returns 轮询任务信息
 */
export const startVideoPollingAPI = async (requestData: StartPollingRequest): Promise<StartPollingRes> => {
    const token = localStorage.getItem("token");
    return request("/api/video/start-polling", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`
        },
        data: JSON.stringify(requestData)
    });
};

/**
 * 查询轮询任务状态
 * @param taskId 任务ID
 * @returns 任务状态信息
 */
export const getPollingStatusAPI = async (taskId: string): Promise<PollingStatusRes> => {
    const token = localStorage.getItem("token");
    return request(`/api/video/polling-status/${taskId}`, {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`
        }
    });
};